﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;

namespace CrmApplication.Models
{
    public class NomModels
	{
		#region RegionMappingNom
		
		public decimal Id { get; set; }
        public string Name { get; set; }

		#endregion

		#region RegionOracleConnect

		private static OracleConnection connect;

        static NomModels()
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["HomeOrcl"].ConnectionString;
            connect = new OracleConnection(connString);
        }
		#endregion

		#region RegionNoms
		/// <summary>
		/// select all data from given table
		/// </summary>
		/// <param name="table"></param>
		/// <returns></returns>
		public static List<NomModels> GetNoms(string table)
        {
            string selectString = "select id_"+table+","+table+"_name from "+"nom"+table;

            OracleCommand loginComm = new OracleCommand(selectString);
            loginComm.Connection = connect;

            List<NomModels> result = new List<NomModels>();
            NomModels result1 = null;
            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result1 = new NomModels();
                        result1.Id=sdr.GetDecimal(0);
                        result1.Name=sdr.GetString(1);
                        result.Add(result1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }

		/// <summary>
		/// create json array 
		/// </summary>
		/// <param name="table"></param>
		/// <returns></returns>
        public static NomContainer GetNomArray(string table)
        {
            NomModels[] data = new NomModels[20];
            data = NomModels.GetNoms(table).ToArray();

            NomContainer container = new NomContainer()
            {
                Records = data,
                Count = data.Length
            };

            return container;
		}
		#endregion
	}

    public class NomContainer
    {
        public NomModels[] Records { get; set; }
        public int Count { get; set; }
    }
}